package com.orange.wwwapi.controller.receipt;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.orange.core.pojo.PageReqDto;
import com.orange.core.pojo.R;
import com.orange.core.pojo.entity.receipt.Receipt;
import com.orange.core.pojo.req.receipt.ReceiptPageReq;
import com.orange.core.pojo.req.receipt.ReceiptSaveReq;
import com.orange.core.service.receipt.ReceiptService;
import com.orange.core.util.PrincipalUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 小票
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/receipt")
public class ReceiptController {

    private final ReceiptService receiptService;

    /**
     * 我的小票分页查询
     */
    @PostMapping("/page")
    public R<Page<Receipt>> page(@RequestBody PageReqDto<ReceiptPageReq> req) {
        if (req.getCondition() == null) {
            req.setCondition(new ReceiptPageReq());
        }
        req.getCondition().setUserId(PrincipalUtil.getId());
        return R.ok(receiptService.page(req));
    }

    /**
     * 上传小票
     */
    @PostMapping("/save")
    public R<Boolean> save(@RequestBody @Validated ReceiptSaveReq req) {
        receiptService.save(req);
        return R.ok();
    }
}
